#include "vector3d.h"
#include <cstdlib>
#include <cmath>
#include <iostream>

Vector3D::Vector3D(){
	x = 0.0;
	y = 0.0;
	z = 0.0;
}

Vector3D::Vector3D(double _x, double _y, double _z):x(_x), y(_y), z(_z){
}

Vector3D::~Vector3D(){
}

double &Vector3D::operator[](unsigned int _i){
    switch(_i){
        case 0:
            return x;
            break;
        case 1:
            return y;
            break;
        case 2:
            return z;
            break;
    }
    std::cout <<"Erro no operador [] de Vector3D!"<<std::endl;
    exit(1);
}

Vector3D& Vector3D::operator=(const Vector3D &_vector){
	if(this != &_vector){
		x = _vector.x;
		y = _vector.y;
		z = _vector.z;
	}
	return *this;
}

Vector3D Vector3D::operator*(double _s) const {
	return Vector3D(x*_s, y*_s, z*_s);
}

Vector3D Vector3D::operator+(const Vector3D &_vector) const {
	return Vector3D(x+_vector.x, y+_vector.y, z+_vector.z);
}

Vector3D Vector3D::operator-(const Vector3D &_vector) const {
	return Vector3D(x-_vector.x, y-_vector.y, z-_vector.z);
}

Vector3D Vector3D::crossProduct(const Vector3D &_vector) const {
	return Vector3D(y*_vector.z - z*_vector.y, z*_vector.x - x*_vector.z, x*_vector.y - y*_vector.x);
}

double Vector3D::dotProduct(const Vector3D &_vector) const {
	return x*_vector.x + y*_vector.y + z*_vector.z;
}

double Vector3D::length() const {
	return sqrt(this->dotProduct((*this)));
}

double Vector3D::length_2() const {
	return this->dotProduct((*this));
}

Vector3D Vector3D::normalize() const{
	double norm = length();
	return Vector3D(x/norm, y/norm, z/norm);
}

double *Vector3D::intoVector() const {
    double *d;
    d = new double[3];
    d[0] = x;
    d[1] = y;
    d[2] = z;

    return d;
}

void Vector3D::print() const {
    std::cout << "[ " <<x << ", " << y << ", " << z <<"]"<<std::endl;
}
